import request from "@/base/request"
import { ElMessage } from "element-plus"
import {ref} from 'vue'

export default function(){

    const index=ref(0);

    interface PeopleJudgeCache{
        id:number,
        userId:number,
        examId:number,
        userName:string,
        input:string,
        score:number
    }

    const peopleJudgeCacheList=ref<Array<PeopleJudgeCache>>([])

    async function getPeopleJudgeCache(examId:number,problemId:number) {
        const response=(await request.get(`/judge/judgeExam/getPeopleJudgeCache/${examId}/${problemId}`)).data
        if(response.code===200){
            peopleJudgeCacheList.value=response.data
        }else{
            ElMessage.warning(`获取缓存失败，建议重试:${response.msg}`)
        }
    }

    async function updateScore(value) { 
        const response=(await request.put('/judge/judgeExam/updateScore',peopleJudgeCacheList.value[index.value])).data
        if(response.code!==200){
            ElMessage.error(`保存失败，请重试`,response.msg);
        }else{
            peopleJudgeCacheList.value[index.value].score=value;
        }
    }



    return {index,peopleJudgeCacheList,updateScore,getPeopleJudgeCache}
}